<template>
	<eui-page-frame  :bottomHeight="96">
		<template #top>
			<view class="content-img">
				<eui-nav-bar :title="title" color="#000000" />
			</view>
			<view class="top-grid">
				<eui-grid :column="4" :showBorder="false" :square="false">
					<eui-grid-item v-for="(item, index) in litsMenu" :index="index" :key="index">
						<view class="grid-item-box" style="background-color: #fff;" @click="openMenu(item)">
							<image class="grid-item-box__img" :src="item.imageUrl" mode="aspectFill" />
							<text class="grid-item-box__text">{{item.title}}</text>
						</view>
					</eui-grid-item>
				</eui-grid>
			</view>
		</template>
		<eui-page-content>

			<!-- <eui-base-card title="洽谈意向"> -->
			<view class="card-new">
				<view class="talk-content">洽谈意向</view>
				<eui-line></eui-line>
				<eui-card-input v-model="dataModel.c_adname" title="负责部门" placeholder="请选择部门" type="text"
					displayable="false" rightIconType="right"  h-padding="0" @iconClick="selectOpen('depart')" />
				<eui-card-input v-model="dataModel.c_talk_username" title="负责人" placeholder="请选择人员" type="text"
					displayable="false" rightIconType="right"  h-padding="0" @iconClick="selectOpen('user')" required />
				<eui-card-input v-model="dataModel.c_talk_process" :range="ProcessSelectItems" title="当前阶段"
					rightIconType="right" type="selector"   h-padding="0" placeholder="选择阶段" alignType="right" required />
				<eui-card-input v-model="dataModel.c_talk_place"  h-padding="0" title="洽谈地点" placeholder="请输入洽谈地点" type="text"
					alignType="right" maxlength="50" />
				<eui-card-input v-model="dataModel.c_talk_mode"  h-padding="0" :range="talkType" title="洽谈方式" rightIconType="right"
					type="selector" placeholder="请选择洽谈方式" alignType="right" required />
				<eui-card-input v-model="dataModel.c_talk_dt" h-padding="0" title="洽谈时间" placeholder="请选择洽谈时间" type="date"
					 rightIconType="right" singleTag="date1" alignType="right" required :showBottomLine="false" />
			</view>
			<!-- </eui-base-card> -->
			<view class="card">
				<view class="talk-content-title">洽谈内容</view>
				<eui-card-input title="" v-model="dataModel.c_talk_memo" placeholder="请输入内容" type="textarea"
					maxlength="200" :showBottomLine="false"  />
			</view>
			<view class="card">
				<view class="talk-content-image">图片上传</view>
				<view style="padding:28rpx;">
					<UploadFile :autoUpload="false" fileMediatype="image" :fileId="dataModel.c_id" fileClass="洽谈记录登记单"
						return-type="array" mode="grid" :fileMap="createAddId(dataModel)" :imageStyles="imageStyle"></UploadFile>
				</view>
			</view>
		</eui-page-content>
		<template #bottom>
			<view class="bt-button">
				<eui-button :disabled="!showButtom" type="primary" title="提交" @click="submit" />
			</view>
		</template>
	</eui-page-frame>

</template>


<script setup lang="ts">
	import { ref,computed } from 'vue'
	import UploadFile from '@/components/UploadFile.vue';
	import { onLoad, onShow } from "@dcloudio/uni-app";
	import { useLoginStore } from '@/store/modules/useLoginStore';
	import { useMainStore } from "@/store/modules/store";
	import useSaveBill from '@/hooks/useSaveBill';
	import useUpload from '@/hooks/useUpload';
	import Guid from '../../../common/guid';
	import { getRelatedName, getDictName, getDictSelectList } from '../components/RelatedName.js';
	import useService from '@/hooks/useService';
	const service = useService({ prefix: "apiApi" });
	const mainService = useService({ prefix: "mainApi" });
	const mainStore = useMainStore();
	const { loginInfo } = mainStore;
	const billId = ref('');
	const title = ref('新增洽谈')
	const fileList = ref([])
	const talkType = getDictSelectList('招商洽谈方式');
	const { save, createId } = useSaveBill({
		prefix: 'apiApi',
		table: {
			main: [
				{
					tableName: "tb_mo_talk",
					primaryKeys: ["c_id", "c_tenant_id"],
				},
			],
			detail: [
				{ tableName: 'tb_mo_talk_article', primaryKeys: ['c_tenant_id', 'c_id', 'c_article_code'] },
				{ tableName: 'tb_mo_talk_process', primaryKeys: ['c_tenant_id', 'c_id', 'c_process_id'] },
				{ tableName: 'tb_mo_talk_cetificate', primaryKeys: ['c_tenant_id', 'c_id', 'c_name'] },
			]
		},
		uniqueKey: "管易通洽谈记录登记单",
		method: "EnjoyBillSave",
		billName: '洽谈记录登记单',
		billType: 'D11'
	});
	const LoginStore = useLoginStore();
	const ProcessSelectItems = ref([]);
	const talkModes = ref([]);
	const talkProcess = ref(-1);
	const litsMenu = ref([
		{ title: '填资料', imageUrl: '/static/img/rdsImgs/zsqt_tzl.png', url: './addInfo' },
		{ title: '谈条款', imageUrl: '/static/img/rdsImgs/zsqt_ttk.png', url: './addTalkTerm' },
		// { title: '推意向', imageUrl: '/static/img/rdsImgs/zsqt_tyx.png' },
		// {title:'传资证',imageUrl:'/static/img/rdsImgs/zsqt_czz.png',url:'./uploadAptitude'},
	])
	const showButtom =  computed(() => {
		return dataModel.value.c_status==='0' && (dataModel.value.c_approve_status==='1' || !dataModel.value.c_approve_status);
	});
	const dataModel = ref(
		{
			c_tenant_id: '',//租户编码
			c_id: '',//单号
			c_bill_type: 'D11',  //单据类型(tb_sc_bill_type中定义)
			c_status: '0',   //单据状态(0-尚未审核,3-已审核,5-已生效,9-已作废)(tb_id_sys定义:s_gl_bill_status)
			c_adno: '',      //部门编码
			c_adname: '',
			c_store_id: loginInfo.sUserStore,  //机构编码
			c_talk_type: '1',   //洽谈类型|1-新签，2-续签
			c_times: 1,        //洽谈轮次
			c_talk_dt: new Date(),      //洽谈时间
			c_talk_userno: '',    //洽谈人
			c_talk_username: '',
			c_provider: '',    //意向商户
			c_provider_name: '',   //商户名称
			c_cooperation: '1',   //合作方式|1-战略，2-联发
			c_con_type: '',       //经营方式
			c_st_dt: null,
			c_en_dt: null,
			c_ccode: '',
			c_bcode: '',
			c_counter_no: '',
			c_link_man: '',
			c_link_man_title: '',
			c_link_tele: '',
			c_talk_place: '',
			c_talk_memo: '',
			c_talk_hours: 1,
			c_talk_mode: '0',
			c_talk_status: '0',
			c_complete: null,
			c_ref_id: '',
			c_src_id: '',
			c_src_type: '',
			c_mk_store_id: loginInfo.sUserStore,
			c_mk_dt: new Date(),
			c_mk_userno: loginInfo.sUser,
			c_approve_status: '1',
			c_approve_dt: null,
			c_mk_type: '0',
			c_au_dt: null,
			c_au_userno: '',
			c_print_userno: '',
			c_print_dt: null,
			c_print_count: 0,
			c_note: '',
			c_sys_note: '',
			c_bname: '',
			c_position: '1',
			c_talk_process: ''
		},
	);
	const isAdd = ref(true);
	const imageStyle = ref({
		"height": '200rpx',	// 边框高度
		"width": '200rpx',	// 边框宽度
		"border": {
			"color":"#EEEEEE",
			"width":"1rpx",
			"style":"dashed",
			"radius":"10rpx"
		}

	})
	const createAddId = async (dataModel) => {
		if (!dataModel.c_id) {
			await createId(dataModel)
		}
	}
	onLoad(async ({ c_id, c_status, c_title }) => {
		if (c_id) {
			if (c_status != '3') {
				isAdd.value = false;
				billId.value = c_id;
				await GetBillItem(c_id);
			}
			else {
				isAdd.value = true;
				await QueryBillData(c_id);
			}
		}

		if (c_title) {
			title.value = c_title;
		}
		await FetchBusinessProcess();
	})
	const openMenu = async (item) => {
		if (isAdd.value) {
			uni.showToast({
				duration: 2000,
				icon: 'none',
				title: '请先保存洽谈信息',
				mask: true,
			});
			return;
		}
		if (item.url !== '') {
			const redirectUrl = item.url + "?c_id=" + dataModel.value.c_id;
			uni.navigateTo({
				url: redirectUrl
			});
		}

	}
	//招商进程定义数据
	const FetchBusinessProcess = async () => {
		const res = await service.request('管易通洽谈记录登记单', 'FetchBusinessProcess', '');
		if (res) {
			ProcessSelectItems.value = res.filter(a => a.c_sort > talkProcess.value).map(item => ({
				value: item.c_id,
				text: item.c_name
			}));
		}
	}
	const SelectionChanged = (type, item) => {
		switch (type) {
			case 'user':
				dataModel.value.c_talk_userno = item.value;
				dataModel.value.c_talk_username = item.text;
				break;
			case 'depart':
				dataModel.value.c_adno = item.value;
				dataModel.value.c_adname = item.text;
				break;
		}
	}
	const selectOpen = (selectType: string) => {
	    uni.navigateTo({
	        url: `../modals/index?type=${selectType}`
	    });
	}

	const openDetail = (e) => {
		uni.navigateTo({
			url: './detail'
		});
	}
	const submit = async () => {
		if (dataModel.value.c_talk_userno === "") {
			uni.showToast({
				duration: 2000,
				icon: 'none',
				title: '请选择负责人',
				mask: true,
			});
			return;
		}
		if (dataModel.value.c_talk_process === "") {
			uni.showToast({
				duration: 2000,
				icon: 'none',
				title: '请选择阶段',
				mask: true,
			});
			return;
		}
		if (dataModel.value.c_talk_mode === "") {
			uni.showToast({
				duration: 2000,
				icon: 'none',
				title: '请选择洽谈方式',
				mask: true,
			});
			return;
		}
		if (!dataModel.value.c_talk_dt) {
			uni.showToast({
				duration: 2000,
				icon: 'none',
				title: '请选择洽谈时间',
				mask: true,
			});
			return;
		}
		uni.showLoading({ title: "正在保存...", mask: true })
		const result = await save(isAdd.value, dataModel.value);
		billId.value = result.BillId;
		uni.hideLoading();
		uni.showToast({
			duration: 2000,
			icon: 'none',
			title: '保存成功'
		});
		setTimeout(async() => {
		  await GetBillItem(result.BillId);
		},1000);
	}
	//获取单据详情
	const GetBillItem = async (id) => {
		const req = {
			BillId: id
		};
		uni.showLoading({ title: "查询中...", mask: true });
		const res = await service.request('管易通洽谈记录登记单', 'EnjoyBillQuery', req);
		uni.hideLoading();
		if (res) {
			dataModel.value = res.ds[0];
			dataModel.value.c_adname = getRelatedName('rdsDeparts', dataModel.value.c_adno);
			dataModel.value.c_talk_username = getRelatedName('rdsUsers', dataModel.value.c_talk_userno);
			billId.value = dataModel.value.c_id;
			isAdd.value = false;
		}
	}
	const QueryBillData = async (c_id : string) => {
		const req = {
			c_id: c_id,
			c_new_bill: "1",
			c_talk_userno:loginInfo.sUser
		}
		uni.showLoading({ title: "查询中...", mask: true });
		const res = await service.request('管易通洽谈记录登记单', 'QueryBillData', req);
		uni.hideLoading();
		if (res) {
			talkProcess.value = res.ds[0]?.c_talk_process_sort;
			//console.log('talkProcess.value',talkProcess.value)
			dataModel.value = res.ds[0];
			dataModel.value.c_talk_dt = new Date();
			dataModel.value.c_mk_store_id = loginInfo.sUserStore;
			dataModel.value.c_mk_dt = new Date();
			dataModel.value.c_mk_userno = loginInfo.sUser;
			dataModel.value.c_approve_status = '1';
			dataModel.value.c_bill_type = 'D11';  //单据类型(tb_sc_bill_type中定义)
			dataModel.value.c_status = '0';
			dataModel.value.c_adname = getRelatedName('rdsDeparts', dataModel.value.c_adno);
			dataModel.value.c_talk_username = getRelatedName('rdsUsers', dataModel.value.c_talk_userno);
		}
	}
	defineExpose({
		SelectionChanged
	})
</script>

<style lang="scss">
	.content {
		display: flex;
		flex: 1;
		flex-direction: column;
		background-color: #F2F2F2;
		padding: 0px !important
	}

	.content-img {
		background-color: #FFFFFF;
		background-size: 100% 420rpx;
		background-repeat: no-repeat;
		width: 100%;
	}

	.navigation {
		flex: 0 0 auto;
		margin-bottom: 5px;
	}

	.top-grid {
		background-color: #FFFFFF;
		display: flex;
		flex-direction: row;
		border-radius: 10px;
		margin: 20rpx 20rpx 0rpx 20rpx;
		padding: 28rpx 0rpx;
		justify-content: space-between;
		justify-items: center;
	}

	.grid-item-box {
		flex: 1;
		// position: relative;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		align-items: center;
		justify-content: center;

		&__img {
			width: 88rpx;
			height: 88rpx;
		}

		&__text {
			font-size: 26rpx;
			margin-top: 16rpx;
		}
	}
	.card-new{
		border-radius: 20rpx;
		padding:  28rpx 28rpx 0rpx 28rpx;
		margin-bottom: 20rpx;
		background: #ffffff;
	}
	.card {
		border-radius: 20rpx;
		padding:  28rpx 0rpx 20rpx 0rpx;
		margin-bottom: 20rpx;
		background: #ffffff;
	}

	.talk-content {
		font-size: 30rpx;
		color: #000000;
		line-height: 42rpx;
		font-weight: 600;
		padding: 0rpx 0rpx 20rpx 0rpx;
	}
	.talk-content-title{
		font-size:30rpx;
		color:#000000;
		line-height:42rpx;
		font-weight:600;
		margin-bottom:-20rpx;
		padding:0rpx 0rpx 0rpx 28rpx;
	}
	.talk-content-image{
		font-size:30rpx;
		color:#000000;
		line-height:42rpx;
		font-weight:600;
		margin-bottom:-10rpx;
		padding:28rpx 0rpx 0rpx 28rpx;
	}
	.bt-button {
		background-color: #FFFFFF;
		padding: 10rpx 0rpx;
		display: flex;
		justify-content: space-around;
		align-items: center;
	}
</style>
